// 用户相关
var router = require('../common');
var userModule = require('../../model/userMode')
const multiparty = require('multiparty');
const uuidV1 = require('uuidv1');
const images = require('images');
// 登录、如果没注册顺便注册了
router.post('/login', async (req, res) => {
    let { tel, sms } = req.body;
    let username = 'qll_' + new Date().getTime();
    console.log(username);
    console.log(tel, sms);
    let return_msg1 = await userModule.findOne({ phone: tel });
    if (!return_msg1) {
        let return_msg2 = await userModule.create({ username, phone: tel, })
        res.json({
            ok: 0,
            msg: '注册成功',
            data: return_msg2
        })
    } else {
        res.json({
            ok: 1,
            msg: '欢迎回来',
            data: return_msg1
        })
    }

});
// 用户上传头像
router.post('/update/head_photo', function (req, res, next) {
    console.log('tupian');
    let user_token = JSON.parse(decodeURI(req.headers.usertoken));
    if (user_token) {
        console.log(user_token);
        let {phone} = user_token; 
        var form = new multiparty.Form();
        form.parse(req,async function(err, fields, files) {
            // console.log(files.file[0])
            // //找到上传的图片上传之前的名字
            var orgFilename = files.file[0].originalFilename;
            // console.log(orgFilename)
            // //切割orgFilename 找到图片的扩展名 以证明图片是什么格式的
            // //切割后是一个数组，找到数组最后一个
            var formate = orgFilename.split(".");
            // //拼接新的图片名称
            var fileName = uuidV1() + "." + formate[formate.length - 1];
            //      
            images(files.file[0].path) //Load image from file 
                .save("public/images/detail/" + fileName, {
                    quality: 1000
                });
            //返回前台存储地址
            var src = "/images/detail/" + fileName;
            let setPhoto = await userModule.updateOne({phone},{ $set: { head_photo: src} });
            res.json({
                status: true,
                msg: src,
                setPhoto
            })
        });
    } else {
        res.status(404).json({
            status: false,
            msg: '没有登录'
        })
    }
});
// 修改用户昵称
router.post('/update/user_name',async function (req, res, next) {
    let user_token = JSON.parse(decodeURI(req.headers.usertoken));
    let {new_name} = req.body;
    let phone = user_token.phone;
    let return_msg = await userModule.updateOne({phone},{$set:{username:new_name}})
    res.json({
        ok:0,
        msg:'修改成功',
        return_msg
    })
});
// 修改用户性别
router.post('/update/sex',async function (req, res, next) {
    let user_token = JSON.parse(decodeURI(req.headers.usertoken));
    let {sex} = req.body;
    let phone = user_token.phone;
    let return_msg = await userModule.updateOne({phone},{$set:{sex:Number(sex)}})
    res.json({
        ok:0,
        msg:'修改成功',
        return_msg
    })
});
// 修改手机号码
router.post('/update/phone',async function (req, res, next) {
    let user_token = JSON.parse(decodeURI(req.headers.usertoken));
    let {new_phone} = req.body;
    let phone = user_token.phone;
    let return_msg = await userModule.updateOne({phone},{$set:{phone:Number(new_phone)}})
    res.json({
        ok:0,
        msg:'修改成功',
        return_msg
    })
});
// 修改支付方式
router.post('/update/pay_type',async function (req, res, next) {
    let user_token = JSON.parse(decodeURI(req.headers.usertoken));
    let {zfb,wx} = req.body;
    let phone = user_token.phone;
    let return_msg = await userModule.updateOne({phone},{$set:{pay_type:{zfb,wx}}})
    res.json({
        ok:0,
        msg:'修改成功',
        return_msg
    })
});
module.exports = router